package com.qcu.controller.admin;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.qcu.entity.CarouselImage;
import com.qcu.service.CarouselImageService;
import com.qcu.service.MinioService;
import com.qcu.util.Response;
import com.qcu.util.ResponseCode;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * @Name: MyComputer
 * @ClassName CarouselController
 * @Description:
 * @date 2025/4/7 10:33
 */
@RestController
@RequestMapping("/admin/carousel")
public class CarouselController {

    @Resource
    private CarouselImageService carouselService;

    @Resource
    private MinioService minioService;

    @PostMapping(consumes = "multipart/form-data")
    public Response save(@RequestPart(required = false) MultipartFile file , @RequestParam String name) {
        try {

            byte[] bytes = file.getBytes();
            String originalFilename = file.getOriginalFilename();
            String type = originalFilename.substring(originalFilename.lastIndexOf(".")+1);
            String url = minioService.upload(originalFilename, bytes, type);
            boolean isOk = false;
                CarouselImage carouselImage = new CarouselImage();
                carouselImage.setName(name);
                carouselImage.setPath(url);
                isOk = carouselService.save(carouselImage);

            return isOk ? Response.success() : Response.failure(ResponseCode.FAILURE);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @GetMapping()
    public Response get() {
        return Response.success(carouselService.list());
    }

    @DeleteMapping("/{id}")
    public Response remove(@PathVariable Integer id) {
        boolean isOk = carouselService.removeById(id);
        return isOk ? Response.success() : Response.failure(ResponseCode.FAILURE);
    }

    @PutMapping(consumes = "multipart/form-data")
    public Response update(@RequestPart(required = false) MultipartFile file,@RequestParam Integer id, @RequestParam(required = false) String name) {
        try {
            CarouselImage carouselImage = new CarouselImage();
            boolean isOk = false;
            if (name != null) {
                carouselImage.setName(name);
            }
            if (file != null) {
                byte[] bytes = file.getBytes();
                String originalFilename = file.getOriginalFilename();
                String type = originalFilename.substring(originalFilename.lastIndexOf(".")+1);
                String url = minioService.upload(originalFilename, bytes, type);
                carouselImage.setPath(url);
            }
            isOk = carouselService.update(carouselImage, new LambdaUpdateWrapper<CarouselImage>().eq(CarouselImage::getId, id));
            return isOk ? Response.success() : Response.failure(ResponseCode.FAILURE);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }


}
